define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/index',
                    add_url: 'goods/add',
                    edit_url: 'goods/edit',
                    del_url: 'goods/del',
                    multi_url: 'goods/multi',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'goods_id', title: __('Goods_id')},
                        // {field: 'cat_id', title: __('Cat_id')},
                        {field: 'category.name', title: __('Category.name')},
                        {field: 'goods_sn', title: __('Goods_sn')},
                        {field: 'goods_name', title: __('Goods_name')},
                        {field: 'goods_name_en', title: __('Goods_name_en')},
                        {field: 'hsnumber', title: __('Hsnumber')},
                        {field: 'property', title: __('Property')},
                        {field: 'gproduction', title: __('Gproduction')},
                        {field: 'brand', title: __('Brand')},
                        {field: 'unit', title: __('Unit')},
                        {field: 'brand_id', title: __('Brand_id')},
                        {field: 'store_count', title: __('Store_count')},
                        {field: 'comment_count', title: __('Comment_count')},
                        {field: 'weight', title: __('Weight')},
                        {field: 'market_price', title: __('Market_price'), operate:'BETWEEN'},
                        {field: 'shop_price', title: __('Shop_price'), operate:'BETWEEN'},
                        {field: 'member_price', title: __('Member_price'), operate:'BETWEEN'},
                        {field: 'cost_price', title: __('Cost_price'), operate:'BETWEEN'},
                        {field: 'keywords', title: __('Keywords')},
                        {field: 'goods_remark', title: __('Goods_remark')},
                        {field: 'original_img', title: __('Original_img')},
                        {field: 'is_on_sale', title: __('Is_on_sale')},
                        {field: 'on_time', title: __('On_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'last_update', title: __('Last_update'),operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'sales_sum', title: __('Sales_sum')},
                        {field: 'commission', title: __('Commission'), operate:'BETWEEN'},
                        {field: 'spu', title: __('Spu')},
                        {field: 'sku', title: __('Sku')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='0']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").selectpicker("refresh");
                    $("#c-pid").change();
                });
                $(document).on("change", "#c-pid", function () {
                    $("#c-cpid option[data-type='0']").prop("selected", true);
                    $("#c-cpid option").removeClass("hide");
                    $("#c-cpid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-cpid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});